#!/bin/sh

# Copyright(c) 2011 OpenDomo Services SL. Licensed under GPL v3 or later
# Isidro Catalan <skarvin@gmail.com>


# ----------------------------------------------------------------------------
# desc: Motion camera files processing
# author: Isidro Catalan <skarvin@gmail.com>, http://www.opendomo.com
# date: February 2010
#
# CHANGES:
#   21-02-2010	skarvin Creation
# ----------------------------------------------------------------------------
if ! test -f "/etc/opendomo/videoConfVars.conf"; then
   cp /etc/opendomo/videoConfVars.conf.orig /etc/opendomo/videoConfVars.conf
fi
. "/etc/opendomo/videoConfVars.conf"

# Check if the motion config file is already set
if ! test -f "/etc/opendomo/motion.conf"; then
   cp /etc/opendomo/motion.conf.orig /etc/opendomo/motion.conf
fi

# Lib config
. "$LIB_CONFIG"
# Obtaining params
MODE="$1"

showUsage ()
{
   echo " Missing parameters!
   
   Usage: 
   $0 [mode] <- mode options [surveillance|supervision|tracking]
   "
}

if [ "$#" -lt 1 ]; then
   showUsage   
elif [ "$MODE" = "surveillance" ]; then
     CONF_FILE="/etc/opendomo/motion.conf"
     # Main Config File
     setValue "process_id_file" "/var/run/motion_$MODE.pid"     
     setValue "ffmpeg_cap_new" "on"
     setValue "locate" "off"  
     setValue "output_motion" "off"
     setValue "output_normal" "first"         
     # Process thread files
     IFS=$'\n'
     MAIN_CF="$CONF_FILE"
     for i in `cat "$MAIN_CF" | grep "^thread"`; do 
         CONF_FILE=`echo $i | cut -c8-100`
         CAMID=`getValue "text_left"`
         setValue "on_motion_detected" "/bin/logevent notice odvideo \"Motion detected on camera [$CAMID]\""
         setValue "on_camera_lost" "/bin/logevent error odvideo \"Camera [$CAMID] has been lost!\""
         setValue "on_picture_save" "/bin/logevent warning odvideo \"Motion detected on camera [$CAMID]\" %f"
         done
elif [ "$MODE" = "supervision" ]; then
     CONF_FILE="/etc/opendomo/motion.conf"
     # Main Config File
     setValue "process_id_file" "/var/run/motion_$MODE.pid"     
     setValue "ffmpeg_cap_new" "off"
     setValue "locate" "off"           
     setValue "output_motion" "off"
     setValue "output_normal" "off"     
     # Process thread files
     IFS=$'\n'
     MAIN_CF="$CONF_FILE"
     for i in `cat "$MAIN_CF" | grep "^thread"`; do 
         CONF_FILE=`echo $i | cut -c8-100`
         setValue "on_motion_detected" ""
     done
elif [ "$MODE" = "tracking" ]; then
     CONF_FILE="/etc/opendomo/motion.conf"
     # Main Config File
     setValue "process_id_file" "/var/run/motion_$MODE.pid"          
     setValue "ffmpeg_cap_new" "off"
     setValue "locate" "on"      
     # Experimental
     setValue "output_motion" "on"
     setValue "output_normal" "off"         
     # Process thread files
     IFS=$'\n'
     MAIN_CF="$CONF_FILE"
     for i in `cat "$MAIN_CF" | grep "^thread"`; do 
         CONF_FILE=`echo $i | cut -c8-100`
         CAMID=`getValue "text_left"`
         setValue "on_motion_detected" "/bin/logevent notice odvideo \"Motion detected on camera [$CAMID]\""
         setValue "on_camera_lost" "/bin/logevent error odvideo \"Camera [$CAMID] has been lost!\""
         setValue "on_picture_save" "/bin/logevent warning odvideo \"Motion detected on camera [$CAMID]\" %f"
     done
else
   echo "Unknown mode specified"
   showUsage
fi
